import { ApiProperty } from '@nestjs/swagger';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { hashSync } from 'bcryptjs';

@Schema({
  timestamps: true,
})
export class User extends Document {
  @ApiProperty({ description: '用户名', example: 'test' })
  @Prop({ required: true, unique: true })
  username: string;

  @ApiProperty({ description: '密码', example: '1234' })
  @Prop({
    required: true,
    select: false,
    get(val) {
      return val;
    },
    set(val) {
      return val ? hashSync(val) : val;
    },
  })
  password: string;

  @ApiProperty({ description: '账号权限', example: '1' })
  @Prop({ required: true, default: 1 })
  grant: number;

  @ApiProperty({ description: '电话', example: '1234' })
  @Prop()
  tel: number;

  @ApiProperty({ description: '地址', example: 'xxx省xxx市xxx' })
  @Prop()
  address: string;
}

export type UserDocument = User & Document;

export const UserSchema = SchemaFactory.createForClass(User);
